﻿using ReadyApps.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ReadyAppsTest.Common
{
    /// <summary>
    ///This is a test class for WebUtilitiesTest and is intended
    ///to contain all WebUtilitiesTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WebUtilitiesTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region FindControlRecursive Tests

        [TestMethod]
        public void FindControlRecursiveOneControlTest()
        {
            Control baseControl = new Page();
            baseControl.ID = "testID";
            string controlID = "testID";
            Control expected = baseControl;
            Control actual;
            actual = WebUtilities.FindControlRecursive(baseControl, controlID);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void FindControlRecursiveNullControlTest()
        {
            Control baseControl = null;
            string controlID = "testID";
            Control actual;
            actual = WebUtilities.FindControlRecursive(baseControl, controlID);
            Assert.IsNull(actual);
        }

        [TestMethod]
        public void FindControlRecursiveTreeTest()
        {
            Control baseControl = new Page();
            baseControl.Controls.Add(new Literal() { ID = "notMe" });
            string controlID = "testID";
            Control expected = new Label() { ID = "testID" };
            baseControl.Controls.Add(expected);
            Control actual;
            actual = WebUtilities.FindControlRecursive(baseControl, controlID);
            Assert.AreSame(expected, actual);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindControlRecursiveNullControlIDTest()
        {
            Control baseControl = new Page();
            baseControl.ID = "testID";
            string controlID = null;
            Control actual;
            actual = WebUtilities.FindControlRecursive(baseControl, controlID);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindControlRecursiveEmptyControlIDTest()
        {
            Control baseControl = new Page();
            baseControl.ID = "testID";
            string controlID = string.Empty;
            Control actual;
            actual = WebUtilities.FindControlRecursive(baseControl, controlID);
        }

        #endregion
    }
}
